package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xyiaao
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2022-02-06 10:55:06
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，并保存对应的口味数据
     *
     * @param dishDto
     */
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味到菜品表dish_flavors
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据 id来查询 菜品信息和口味信息
     *
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 根据 id来修改菜品信息和口味信息
     *
     * @param dishDto
     */

    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据ids来删除菜品信息和口味信息
     *
     * @param ids
     */
    public void deleteWithFlavor(List<Long> ids) {

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids).eq(Dish::getStatus,1);
        int count = this.count(qw);
        if (count>0) {
            throw new CustomException("菜品正在售卖中，无法删除");
        }

        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId, ids);

        dishFlavorService.remove(lqw);
    }

    @Override
    public Integer countSaleNum(Long id, String beginTime, String endTime) {

        // 先查菜品单独销量
        Integer dishSaleNum = dishMapper.countSaleNum(id, beginTime, endTime);
        // 再查此菜品在套餐中的销量
        Integer setmealDishSaleNum = dishMapper.countSetmealDishSaleNum(id, beginTime, endTime);
        // 将二者相加
        return (dishSaleNum == null ? 0 : dishSaleNum) + (setmealDishSaleNum == null ? 0 : setmealDishSaleNum);
    }
}
